﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApplication1.RssFeedService;

namespace WPFClient
{
    /// <summary>
    /// Interaction logic for subscribeWin.xaml
    /// </summary>
    public partial class subscribeWin : Window
    {
        public utils u;
        public subscribeWin(utils _u)
        {
            InitializeComponent();
            u = _u;
            this.Visibility = System.Windows.Visibility.Visible;
        }

        public void rssFeed_SubscribeCompleted(Object sender, SubscribeCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                u.rssWin.listFeed();
                this.Close();
            }
            else
            {
                lerror.Content = "Une erreur est survenue";
                lerror.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            RssFeed feed = new RssFeed();
            feed.id = u.RssDico.Count;
            feed.url = tbadresse.Text;
            feed.link = tbsite.Text;
            feed.description = tbdescription.Text;
            feed.date = DateTime.Now;
            if (checkBox1.IsChecked == true)
                feed.@public = true;
            else
                feed.@public = false;
            if (tbadresse.Text.Length == 0 || tbsite.Text.Length == 0)
            {
                lerror.Content = "Un ou plusieurs champs sont incorrects";
                lerror.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                u.getRfs().SubscribeCompleted += new EventHandler<SubscribeCompletedEventArgs>(rssFeed_SubscribeCompleted);
                u.getRfs().SubscribeAsync(u.getSession(), feed);
            }
        }
    }
}
